package com.tff.demo.jdbc.bean;

import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class BeanFactory {
    //定义一个properties对象
    private static Properties pros;
    //定义容器来保存创建的对象
    private static Map<String,Object> map;

    //使用静态代码块为properties复制
    static {
        try {
            pros = new Properties();
            //初始化容器
            map = new HashMap<String, Object>();
            //获取properties刘对象的
            InputStream in = BeanFactory.class.getClassLoader().getResourceAsStream("bean.properties");
            pros.load(in);
            //取出配置文件中所有key
            Enumeration keys = pros.keys();
            //遍历枚举
            while (keys.hasMoreElements()){
                //取出每个key
                String key = keys.nextElement().toString();
                //根据key获取value值
                String beanPath = pros.getProperty(key);
                Object value = Class.forName(beanPath).newInstance();
                map.put(key,value);
            }

        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public static Object getBean(String beanName){
        return map.get(beanName);
    }

    //根据bean获取bean对象

    /**
     *   tff
     * @param beanName 类的全路径名
     * @return  返回一个bean对象
     */
    /*public static Object getBean(String beanName){
        Object bean = null;
        try{
            String beanPath = pros.getProperty(beanName);
            bean = Class.forName(beanPath).newInstance();
        }catch (Exception e){
            e.printStackTrace();
        }

        return bean;
    }*/

}
